<?php

jimport('joomla.application.component.controller');

class bgController extends JController
{
    public function display($cachable = false, $urlparams = false)
    {
        parent::display($cachable, $urlparams);
    }

    public function add()
    {
        $document = JFactory::getDocument();
        $model = $this->getModel();
        parent::display();
    }

    public function edit()
    {
        $model = $this->getModel();
        
        $list = JRequest::getVar('cid');
        $id = $list[0];
        
        $details = $model->getDetails($id);
        
        
        parent::display();
    }

    public function view()
    {
        parent::display();
    }
    
    public function remove(){
        $list = JRequest::getVar('cid');
        $model = $this->getModel();
        $model->deleteGroup($list);
        
        $this->setRedirect('index.php?option=com_mail_group');
    }

    public function save()
    {
        $data = JRequest::getVar('cid');
        $name = JRequest::getVar('g_name');
        if(empty($name)){
                    $error = "グループ名を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
        } else {
           $type = JRequest::getVar('type');
           $action = JRequest::getVar('action');
           $model = $this->getModel();
           $group = "";
               
           switch($type){
               case 0:
               case 1:
               case 3:
               case 4:
                   $group = array(
                       'mode' => $type,
                       'name' => $name,
                       'data' => $data,
                       );
                   break;
               case 2:
                   $group = array(
                       'mode' => $type,
                       'name' => $name,
                       );
                   $result = HelperBG::readCSV($_FILES['filecsv']['tmp_name']);

                   
                   foreach($result as $key=>$row){
                       //skip first row
                       if($key == 0){
                           $i++;
                           continue;
                       }
                       //skip row with empty email address
                       if(empty($row[1]))
                           continue;
                       //if 'name' not found, get first part of email as name
                       if(empty($row[0])){
                           $tmp = explode("@",$row[1]);
                           $row[0] = $tmp[0];
                       }                     
                       $group['data'][$key] = array(
                                   'name' => $row[0],
                                   'email' => $row[1],
                       );        
                   }
                   break;
           }        
           switch($action) {
               case 'new' :
                   $model->addGroup($group);
                   $this->setRedirect('index.php?option=com_mail_group');
                   break;
               case 'update':
                   //dump($group);
                   $group['code'] = JRequest::getVar('old_code');
                   $model->updateGroup($group);
                   $this->setRedirect('index.php?option=com_mail_group');
                   break;
               
           }
           
           
        }
        parent::display();
    }

    public function ajax_get_pref()
    {
        $model = $this->getModel();
        $prefs = $model->getPrefsByArea(JRequest::getVar('area_code'));
        echo "<option value='all'>全て</option>";
        foreach ($prefs as $pref) {
            echo "<option value='$pref->PREF_CODE'>$pref->PREF_NAME</option>";
        }
        $app = &JFactory::getApplication();
        $app->close();

    }

    public function ajax_search()
    {
        $filter = array(
            'keyword' => JRequest::getVar('keyword'),
            'type' => JRequest::getVar('type'),
            'area' => JRequest::getVar('area'),
            'pref' => JRequest::getVar('pref'),
            );
        $model = $this->getModel();
        $lstuser = $model->searchUser($filter);
        if (count($lstuser) > 0)
          if($filter['type'] == 1){
            echo "
                    <thead>
                      <tr>
                        <th  width='1%'>
                          <input type='checkbox' name='checkall-toggle' value='' title='" . JText::_('JGLOBAL_CHECK_ALL') . "' onclick='Joomla.checkAll(this)' />
                        </th>
                        <th class='left'>ユーザ名 </th>
                        <th class='left'>自宅メールアドレス</th>
                        <th class='left'>携帯メールアドレス</th>
                        <th class='left'>勤務先メールアドレス</th>
                      </tr>
                    </thead>
                    <tbody >";
                    foreach ($lstuser as $key => $user) {
                        $row_cls = $key % 2;
                        $mail = $user->mail_flag == '1' ? $user->email : '拒む';
                        $m_mail = $user->m_mail_flag == '1' ? $user->m_email : '拒む';
                        $w_mail = $user->w_mail_flag == '1' ? $user->w_email : '拒む';
                        echo "<tr class='row$row_cls'>
                      <td class='left'><input type='checkbox' id='cb0' name='cid[]' value='$user->id'/></td>
                      <td>$user->name</td>
                      <td class='left' style='width: 300px'>$mail</td>
                      <td class='left' style='width: 300px'>$m_mail</td>
                      <td class='left' style='width: 300px'>$w_mail</td>
                      </tr>";
            echo "</tbody>
                  ";
              
              }
          } else {
            echo "
                    <thead>
                      <tr>
                        <th  width='1%'>
                          <input type='checkbox' name='checkall-toggle' value='' title='" . JText::_('JGLOBAL_CHECK_ALL') . "' onclick='Joomla.checkAll(this)' />
                        </th>
                        <th class='left'>ユーザ名 </th>
                        <th class='left'>メールアドレス</th>
                      </tr>
                    </thead>
                    <tbody >";
                    foreach ($lstuser as $key => $user) {
                        $row_cls = $key % 2;
                        echo "<tr class='row$row_cls'>
                      <td class='left'><input type='checkbox' id='cb0' name='cid[]' value='$user->id'/></td>
                      <td>$user->name</td>
                      <td class='left'>$user->email</td>
                      </tr>";
            echo "</tbody>
                  ";
              
              }
          }
          
        $app = &JFactory::getApplication();
        $app->close();

    }

    public function search(){

        parent::display();
    }
    
    
}
